﻿using can.Achieve;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Intrinsics.X86;
using System.Text;
using System.Threading.Tasks;
using static can.Achieve.CANCommunitor;

namespace can
{
    public class Can
    {
        //设备类型，设备索引，can通道索引
        private const uint DeviceType = 4;
        private const uint DeviceIndex = 0;
        private const uint CanIndex = 0;
        public static async Task Main(string[] args)
        {
            //尝试打开设备
            if (VCI_OpenDevice(DeviceType, DeviceIndex, 1) != 1)
            {
                Console.WriteLine("开启通道失败");
            }
            else
            {
                Console.WriteLine("开启通道成功");
            }
            //初始化CAN通道
            VCI_INIT_CONFIG initConfig = new VCI_INIT_CONFIG
            {
                AccCode = 0x00000000,
                AccMask = 0xFFFFFFFF,
                Reserved = 0,
                Filter = 1,
                Timing0 = 0x03,
                Timing1 = 0x1C,
                Mode = 0,
            };
            if (VCI_InitCAN(DeviceType, DeviceIndex, CanIndex, ref initConfig) != 1)
            {
                Console.WriteLine("初始化失败");
            }
            else
            {
                Console.WriteLine("初始化成功");
            }
            //启动CAN通道
            if (StartCAN())
            {
                Console.WriteLine("启动CAN通信失败，程序结束。");
                return;
            }
            //发送开关电源命令
            Console.WriteLine("发送电源开关命令");

            // 获取电源状态并输出
            Console.WriteLine("电源开启前各模块电源状态：------------------");
            StartReceiving();

            await InitAllInfo();

            int module1 = 1; 
            int module2 = 2; 
            int module3 = 3; 
            bool power = true; // true表示0x00开启电源，false表示0x01关闭电源

            ValueTask<bool> task1 = SetPower(module1, power);
            ValueTask<bool> task2 = SetPower(module2, power);
            ValueTask<bool> task3 = SetPower(module3, power);

            // ValueTask中没有WhenAll方法，将 ValueTask 转换为 Task
            Task<bool> task1AsTask = task1.AsTask();
            Task<bool> task2AsTask = task2.AsTask();
            Task<bool> task3AsTask = task3.AsTask();

            // 使用 Task.WhenAll 等待所有任务完成
            await Task.WhenAll(task1AsTask, task2AsTask, task3AsTask);

            // 获取结果
            bool result1 = await task1;
            bool result2 = await task2;
            bool result3 = await task3;

            Console.WriteLine($"电源模块{module1}控制结果: {result1}");
            Console.WriteLine($"电源模块{module2}控制结果: {result2}");
            Console.WriteLine($"电源模块{module3}控制结果: {result3}");

           // 获取电源状态并输出
           var powerStatusResultAfter = await GetPowerStatus();
            Console.WriteLine("电源开启后各模块电源状态：------------------");
            for (int i = 0; i < powerStatusResultAfter.Length; i++)
            {
                Console.WriteLine($"模块 {i + 1} 电源状态：{(powerStatusResultAfter[i] ? "开" : "关")}");
            }

            //关闭CAN通道
            //VCI_CloseDevice(DeviceType, DeviceIndex);
            //Console.WriteLine("电源控制完毕，设备已关闭");
        }
        //发送电源控制命令
        /*private static VCI_CAN_OBJ BuildSendMsg(uint id, byte[] data)
        {
            return new VCI_CAN_OBJ
            {
                ID = id, // 扩展帧 ID (29 位)
                ExternFlag = 1, // 标记为扩展帧
                RemoteFlag = 0, // 数据帧
                SendType = 0, // 正常发送
                DataLen = 8, // 数据长度
                Data = data,
                Reserved = new byte[3] // 保留
            };
        }
        private static bool SendMsg(uint id, byte[] data)
        {
            VCI_CAN_OBJ send = BuildSendMsg(id, data);
            return VCI_Transmit(DeviceType, DeviceIndex, CanIndex, ref send, 1) == 0;
        }*/
        /*private static void SendPowerControlCommand(byte command)
        {
           VCI_CAN_OBJ send = new VCI_CAN_OBJ
           {
               ID = PowerControlCANID,//扩展帧ID（29位）
               SendType = 0,
               DataLen = 1,
               ExternFlag = 0,//标记为扩展帧
               RemoteFlag = 0,//数据帧
               Data = new byte[] { 0x01 },
               Reserved = new byte[3]
           };
           int result = VCI_Transmit(DeviceType, DeviceIndex, CanIndex, ref send, 1);
           if (result == 0)
           {
               Console.WriteLine($"发送电源控制命令：{(command == PowerOnCommand ? "开" : "关")}");
           }
           else
           {
               Console.WriteLine("发送电源控制命令失败");
           }
        }*/
        /*private static bool SendMsg(uint id, byte[] data) 
        {
            VCI_CAN_OBJ send = SendPowerControlCommand(id, data);
            return VCI_Transmit(DeviceType, DeviceIndex,CanIndex, ref send, 1) ==0;
        }*/
    }


}
